
import { baiduCountMobileArr } from '~/utils/BaiDuCount.js'
export default ({ app: { router }, store }) => {
  /* 每次路由变更时进行pv统计 */
  router.afterEach((to, from) => {
    /* 告诉增加一个PV */
    if (process.browser) {
      if (to.path.substring(0, 9) === '/activity') {
        try {
          window._hmt = window._hmt || []
          window._hmt.push(['_trackPageview', to.fullPath])
        } catch (e) {
          console.log(e)
        }
        baiduCountAllCode(baiduCountMobileArr[1].code)
      } else {
        /* 平台主域名统计*/
        baiduCountAllCode(baiduCountMobileArr[0].code)
      }
    }
  })
}

function baiduCountAllCode(code) {
  var _hmt = _hmt || [];
  (function() {
    // 会叠加 需要 每次执行前，先移除上次插入的代码
    document.getElementById('g_baidu') && document.getElementById('g_baidu').remove()
    var hm = document.createElement('script')
    hm.src = 'https://hm.baidu.com/hm.js?' + code
    hm.id = 'g_baidu'
    document.body.appendChild(hm)
  })()
}
